<template>
    <div class="comment-container">
        <div class="comment-item" :class="{ 'replying': replyingTo === comment.ID }"
            @contextmenu="handleContextMenu($event, comment)">
            <div class="comment-avatar">
                <el-avatar :size="28" :src="comment.avatar" shape="circle">
                    {{ getAvatarText(comment.userName) }}
                </el-avatar>
            </div>
            <div class="comment-content">
                        <div class="comment-header">
            <span class="comment-author">
                {{ comment.userName || '匿名用户' }}
                <span v-if="comment.replyToAuthor || comment.replyTo" class="reply-to">
                    回复 @{{ comment.replyToAuthor || '未知用户' }}
                </span>
            </span>
            <span class="comment-time">{{ formatTime(comment.time) }}</span>
        </div>
        <div class="comment-text" v-html="formatCommentText(comment.checkDailyComments || '')" />
            </div>
            <div class="comment-actions" v-if="replyingTo !== comment.ID">
                <el-tooltip content="回复此评论" placement="top">
                    <el-button size="small" text @click="handleReply">回复</el-button>
                </el-tooltip>
            </div>
        </div>

        <!-- 递归渲染子评论（区域统一控制折叠，此处默认展开） -->
        <div class="comment-replies" v-if="comment.replies && comment.replies.length">
            <CommentItem v-for="reply in comment.replies" :key="reply.ID" :comment="reply" :replying-to="replyingTo"
                :replying-to-author="replyingToAuthor" @reply-click="emitReplyClick" />
        </div>
    </div>
</template>

<script setup lang="ts">
// 时间处理函数已内联，无需导入

export interface Comment {
    ID: number
    userName?: string
    checkDailyComments?: string
    time?: string
    avatar?: string
    replyTo?: number
    replyToAuthor?: string
    replies?: Comment[]
}

const props = defineProps<{
    comment: Comment
    replyingTo?: number
    replyingToAuthor?: string
}>()

// 新增获取头像文本的函数
const getAvatarText = (author?: string) => {
    if (!author || !author.trim()) return ''
    // 获取第一个字符（处理可能的多字节字符）
    return [...author][0]
}
const emit = defineEmits<{
    (e: 'reply-click', comment: Comment): void
}>()

const formatTime = (timeString?: string) => {
    if (!timeString) return ''
    
    try {
        // 直接使用原始时间字符串创建日期对象，让浏览器自动处理时区
        const date = new Date(timeString)
        if (isNaN(date.getTime())) {
            return timeString
        }
        
        // 获取当前日期（本地时间）
        const now = new Date()
        const today = new Date(now.getFullYear(), now.getMonth(), now.getDate())
        const commentDate = new Date(date.getFullYear(), date.getMonth(), date.getDate())
        
        // 判断是否是今天
        if (commentDate.getTime() === today.getTime()) {
            // 今天显示日期和时间
            const year = date.getFullYear()
            const month = String(date.getMonth() + 1).padStart(2, '0')
            const day = String(date.getDate()).padStart(2, '0')
            const time = date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })
            return `${year}-${month}-${day} ${time}`
        } else {
            // 其他日期显示完整的日期和时间
            const year = date.getFullYear()
            const month = String(date.getMonth() + 1).padStart(2, '0')
            const day = String(date.getDate()).padStart(2, '0')
            const time = date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })
            return `${year}-${month}-${day} ${time}`
        }
    } catch (error) {
        console.error('CommentItem: 时间转换错误:', error, '原始时间:', timeString)
        return timeString
    }
}



const formatCommentText = (text?: string) => {
    if (!text) return ''
    // 限制显示长度，超出部分显示省略号
    const maxLength = 200
    const displayText = text.length > maxLength ? text.substring(0, maxLength) + '...' : text
    return displayText.replace(/@(\w+)/g, '<span class="mention">@$1</span>')
}

const handleReply = () => {
    emit('reply-click', {
        ...props.comment,  // 携带原评论所有信息
        replyTo: props.comment.ID,  // 设置replyTo为当前评论ID
        replyToAuthor: props.comment.userName  // 设置replyToAuthor为当前评论作者
    })
}
const emitReplyClick = (comment: Comment) => {
    emit('reply-click', {
        ...comment,  // 携带子评论所有信息
        replyTo: comment.replyTo || props.comment.ID,  // 保持原有replyTo或设置为父评论ID
        replyToAuthor: comment.replyToAuthor || props.comment.userName  // 保持原有replyToAuthor或设置为父评论作者
    })
}

const handleContextMenu = (e: MouseEvent, comment: Comment) => {
    e.preventDefault()
    emit('reply-click', comment)
}

// 折叠由父级整体控制，这里不做局部折叠
</script>

<style scoped>
/* 评论容器动画 */
.comment-container {
    width: 100%;
    margin-bottom: 12px;
    animation: fadeInUp 0.4s ease-out;
}

@keyframes fadeInUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* 评论项主体样式 */
.comment-item {
    display: flex;
    align-items: flex-start;
    padding: 16px 20px;
    background: linear-gradient(135deg, #ffffff 0%, #f8fafc 100%);
    border-radius: 16px;
    border: 1px solid #e2e8f0;
    box-shadow: 
        0 4px 20px rgba(0, 0, 0, 0.08),
        0 1px 4px rgba(0, 0, 0, 0.05),
        inset 0 1px 0 rgba(255, 255, 255, 0.8);
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    width: 100%;
    box-sizing: border-box;
    min-height: 56px;
    position: relative;
    overflow: hidden;
    backdrop-filter: blur(10px);
}

/* 添加装饰性渐变边框 */
.comment-item::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 3px;
    background: linear-gradient(90deg, 
        #3b82f6 0%, 
        #8b5cf6 25%, 
        #06b6d4 50%, 
        #10b981 75%, 
        #f59e0b 100%);
    opacity: 0;
    transition: opacity 0.3s ease;
}

/* 悬停效果增强 */
.comment-item:hover {
    transform: translateY(-1px);
    box-shadow: 
        0 6px 20px rgba(0, 0, 0, 0.08),
        0 2px 6px rgba(0, 0, 0, 0.06),
        inset 0 1px 0 rgba(255, 255, 255, 0.9);
    border-color: #cbd5e1;
    background: linear-gradient(135deg, #ffffff 0%, #f8fafc 100%);
}

.comment-item:hover::before {
    opacity: 0.3;
}

/* 回复状态样式 */
.comment-item.replying {
    background: linear-gradient(135deg, #eff6ff 0%, #dbeafe 100%);
    border-color: #3b82f6;
    box-shadow: 
        0 8px 32px rgba(59, 130, 246, 0.2),
        0 2px 8px rgba(59, 130, 246, 0.1),
        inset 0 1px 0 rgba(255, 255, 255, 0.8);
    transform: scale(1.01);
}

.comment-item.replying::before {
    opacity: 1;
    background: linear-gradient(90deg, #3b82f6, #1d4ed8);
}

/* 头像样式增强 */
.comment-avatar {
    margin-right: 12px;
    flex-shrink: 0;
    position: relative;
}

.comment-avatar::after {
    content: '';
    position: absolute;
    top: -2px;
    left: -2px;
    right: -2px;
    bottom: -2px;
    background: linear-gradient(45deg, #3b82f6, #8b5cf6, #06b6d4);
    border-radius: 50%;
    z-index: -1;
    opacity: 0;
    transition: opacity 0.3s ease;
}

.comment-item:hover .comment-avatar::after {
    opacity: 0.1;
}

/* 评论内容区域 */
.comment-content {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 6px;
}

/* 评论头部样式 */
.comment-header {
    display: flex;
    flex-direction: column;
    font-size: 13px;
    line-height: 1.3;
    position: relative;
    width: 100%;
    margin-bottom: 8px;
}

/* 作者名称样式 */
.comment-author {
    font-weight: 700;
    color: #1e293b;
    white-space: nowrap;
    overflow: visible;
    text-overflow: ellipsis;
    max-width: 180px;
    font-size: 15px;
    background: linear-gradient(135deg, #1e293b 0%, #475569 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    position: relative;
    flex: 1;
    min-width: 0;
}

/* 作者名称中的回复标签样式重置 */
.comment-author .reply-to {
    -webkit-background-clip: initial !important;
    -webkit-text-fill-color: initial !important;
    background-clip: initial !important;
    color: #64748b !important;
    background: linear-gradient(135deg, rgba(100, 116, 139, 0.1) 0%, rgba(100, 116, 139, 0.05) 100%) !important;
}

/* 回复标签样式 */
.reply-to {
    color: #64748b;
    margin-left: 6px;
    font-weight: 500;
    font-size: 12px;
    background: linear-gradient(135deg, rgba(100, 116, 139, 0.1) 0%, rgba(100, 116, 139, 0.05) 100%);
    padding: 3px 8px;
    border-radius: 8px;
    border: 1px solid rgba(100, 116, 139, 0.2);
    box-shadow: 0 1px 3px rgba(100, 116, 139, 0.1);
    transition: all 0.2s ease;
}

.reply-to:hover {
    background: linear-gradient(135deg, rgba(100, 116, 139, 0.12) 0%, rgba(100, 116, 139, 0.06) 100%);
    transform: scale(1.02);
}

/* 评论底部样式 */
.comment-footer {
    display: flex;
    justify-content: flex-end;
    margin-top: 8px;
    padding-top: 4px;
    border-top: 1px solid rgba(148, 163, 184, 0.1);
}

/* 时间样式 */
.comment-time {
    color: #94a3b8;
    font-size: 11px;
    font-weight: 500;
    background: rgba(148, 163, 184, 0.1);
    padding: 2px 6px;
    border-radius: 6px;
    transition: all 0.2s ease;
    margin-top: 4px;
    align-self: flex-start;
    white-space: nowrap;
}

.comment-time:hover {
    background: rgba(148, 163, 184, 0.15);
    color: #64748b;
}

/* 评论文本样式 */
.comment-text {
    font-size: 14px;
    line-height: 1.8;
    color: #334155;
    word-break: break-word;
    margin-top: 4px;
    position: relative;
    padding: 12px 16px;
    background: linear-gradient(135deg, #f8fafc 0%, #f1f5f9 100%);
    border-radius: 10px;
    border: 1px solid #e2e8f0;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
    transition: all 0.2s ease;
}

.comment-item:hover .comment-text {
    background: linear-gradient(135deg, #ffffff 0%, #f8fafc 100%);
    border-color: #cbd5e1;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.08);
}

/* 为评论文本添加装饰性引号 */
.comment-text::before {
    content: '"';
    position: absolute;
    top: 4px;
    left: 4px;
    font-size: 20px;
    color: #cbd5e1;
    font-family: serif;
    opacity: 0.5;
    line-height: 1;
}

.comment-text::after {
    content: '"';
    position: absolute;
    bottom: 4px;
    right: 4px;
    font-size: 20px;
    color: #cbd5e1;
    font-family: serif;
    opacity: 0.5;
    line-height: 1;
}

/* 操作按钮区域 */
.comment-actions {
    margin-left: 10px;
    flex-shrink: 0;
}

.comment-actions .el-button {
    padding: 4px 10px;
    font-size: 12px;
    border-radius: 8px;
    transition: all 0.2s ease;
    background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%);
    border: 1px solid #cbd5e1;
}

.comment-actions .el-button:hover {
    background: linear-gradient(135deg, #3b82f6 0%, #1d4ed8 100%);
    color: white;
    transform: translateY(-0.5px);
    box-shadow: 0 2px 8px rgba(59, 130, 246, 0.2);
}

/* 回复列表样式 */
.comment-replies {
    margin-left: 40px;
    padding-left: 16px;
    border-left: 3px solid transparent;
    background: linear-gradient(to bottom, #e2e8f0, transparent) left/3px 100% no-repeat;
    border-radius: 0 0 0 12px;
    max-height: none;
    overflow: visible;
    margin-top: 12px;
    position: relative;
    padding-top: 8px;
}

/* 回复连接线装饰 */
.comment-replies::before {
    content: '';
    position: absolute;
    left: -3px;
    top: 0;
    width: 3px;
    height: 24px;
    background: linear-gradient(to bottom, 
        #3b82f6 0%, 
        #8b5cf6 30%, 
        #06b6d4 60%, 
        #e2e8f0 100%);
    border-radius: 0 0 0 3px;
    box-shadow: 0 2px 8px rgba(59, 130, 246, 0.2);
}

/* 提及用户样式 */
.mention {
    color: #3b82f6;
    font-weight: 700;
    background: linear-gradient(135deg, 
        rgba(59, 130, 246, 0.15) 0%, 
        rgba(59, 130, 246, 0.08) 100%);
    border-radius: 8px;
    padding: 3px 8px;
    border: 1px solid rgba(59, 130, 246, 0.3);
    box-shadow: 
        0 2px 6px rgba(59, 130, 246, 0.15),
        inset 0 1px 0 rgba(255, 255, 255, 0.5);
    transition: all 0.2s ease;
    position: relative;
    display: inline-block;
}

.mention:hover {
    background: linear-gradient(135deg, 
        rgba(59, 130, 246, 0.2) 0%, 
        rgba(59, 130, 246, 0.12) 100%);
    transform: scale(1.02);
    box-shadow: 
        0 2px 8px rgba(59, 130, 246, 0.2),
        inset 0 1px 0 rgba(255, 255, 255, 0.6);
}

/* 回复切换按钮 */
.replies-toggle {
    margin-left: 40px;
    padding-left: 16px;
    margin-top: 6px;
}

/* 响应式设计 */
@media (max-width: 768px) {
    .comment-item {
        padding: 12px 16px;
        border-radius: 12px;
        /* 确保评论卡片不会遮挡滚动条 */
        margin-right: 8px;
        box-sizing: border-box;
    }
    
    .comment-replies {
        margin-left: 32px;
        padding-left: 12px;
    }
    
    .comment-header {
        margin-bottom: 6px;
    }
    
    .comment-author {
        max-width: 100%;
        font-size: 14px;
        flex-shrink: 1;
        white-space: normal;
        overflow: visible;
    }
    
    /* 移动端回复标签优化 */
    .reply-to {
        font-size: 9px;
        margin-left: 3px;
        padding: 1px 4px;
        border-radius: 4px;
        white-space: nowrap;
        display: inline-block;
        color: #64748b !important;
        background: linear-gradient(135deg, rgba(100, 116, 139, 0.1) 0%, rgba(100, 116, 139, 0.05) 100%) !important;
    }
    
    /* 移动端评论底部优化 */
    .comment-footer {
        margin-top: 6px;
        padding-top: 3px;
        border-top: 1px solid rgba(148, 163, 184, 0.08);
    }
    
    /* 移动端评论时间优化 */
    .comment-time {
        font-size: 7px;
        padding: 1px 4px;
        background: rgba(148, 163, 184, 0.08);
        border-radius: 4px;
        white-space: nowrap;
        margin-top: 2px;
        align-self: flex-start;
    }
    
    /* 移动端评论内容区域优化 */
    .comment-content {
        padding-right: 4px;
    }
    
    .comment-text {
        padding-right: 8px;
    }
}

/* 超小屏幕设备优化 */
@media (max-width: 480px) {
    .comment-header {
        margin-bottom: 4px;
    }
    
    .comment-author {
        max-width: 100%;
        font-size: 13px;
        white-space: normal;
        overflow: visible;
    }
    
    .reply-to {
        font-size: 8px;
        margin-left: 2px;
        padding: 1px 3px;
        border-radius: 3px;
        white-space: nowrap;
        display: inline-block;
        color: #64748b !important;
        background: linear-gradient(135deg, rgba(100, 116, 139, 0.1) 0%, rgba(100, 116, 139, 0.05) 100%) !important;
    }
    
    .comment-footer {
        margin-top: 4px;
        padding-top: 2px;
        border-top: 1px solid rgba(148, 163, 184, 0.06);
    }
    
    .comment-time {
        font-size: 6px;
        padding: 1px 3px;
        background: rgba(148, 163, 184, 0.06);
        border-radius: 3px;
        white-space: nowrap;
        margin-top: 2px;
        align-self: flex-start;
    }
    
    .comment-header {
        font-size: 12px;
    }
}

/* 深色模式支持 */
@media (prefers-color-scheme: dark) {
    .comment-item {
        background: linear-gradient(135deg, #1e293b 0%, #0f172a 100%);
        border-color: #334155;
        color: #e2e8f0;
    }
    
    .comment-item:hover {
        background: linear-gradient(135deg, #334155 0%, #1e293b 100%);
        border-color: #3b82f6;
    }
    
    .comment-author {
        background: linear-gradient(135deg, #e2e8f0 0%, #cbd5e1 100%);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
        background-clip: text;
    }
    
    .comment-text {
        color: #cbd5e1;
        background: linear-gradient(135deg, #1e293b 0%, #0f172a 100%);
        border-color: #334155;
    }
    
    .comment-item:hover .comment-text {
        background: linear-gradient(135deg, #334155 0%, #1e293b 100%);
        border-color: #475569;
    }
    
    .comment-text::before,
    .comment-text::after {
        color: #475569;
    }
}
</style>